package com.ejuornal.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.Editor;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.impl.EditorDaoImpl;
import com.ejuornal.service.EditorService;

@Component("editorService")
public class EditorServiceImpl implements EditorService {
	private EditorDaoImpl editorDao;

	public void add(Editor editor) {
		String sql = "INSERT INTO t_editor VALUES(?, ?, ? ,?, ? ,?)";
		editorDao.add(
				sql,
				new Object[] { 
						null, 
						editor.getPeriodicalId(), 
						editor.getUserId(),
						editor.getStatus(),
						editor.getWorkDate(), 
						null });
	}

	public void update(Editor editor) {
		String sql = "UPDATE t_editor SET status = ?, leaveDate = ? WHERE editorId = ?";
		editorDao.update(sql,
				new Object[] { editor.getStatus(),
						new Date(),
						editor.getEditorId() });
	}

	public Editor findById(int userId) {
		Editor editor = editorDao.findById(userId);

		return editor;
	}

	public List<Editor> findByPeriodicalId(int periodicalId) {
		return editorDao.findByPeriodicalId(periodicalId);
	}

	public Page<Editor> findByProperty(int current_record, String url, String prop, Object value) {
		String hsql = "WHERE " + prop + " = ?";

		return editorDao.find(current_record, url, hsql, new Object[]{ value });
	}

	public EditorDaoImpl getEditorDao() {
		return editorDao;
	}

	@Resource(name = "editorDao")
	public void setEditorDao(EditorDaoImpl editorDao) {
		this.editorDao = editorDao;
	}
}
